<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 搜索栏 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="queroInfo.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="16">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="goodsData" border fit>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" show-overflow-tooltip></el-table-column>
        <el-table-column
          prop="goods_price"
          label="商品价格（元）"
        ></el-table-column>
        <el-table-column prop="goods_number" label="商品数量"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" @click="del(scope.row.goods_id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queroInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queroInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- dislog -->
    <el-dialog
      title="提示"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="clearGoodsForm"
    >
      <el-form
        ref="goodsFormRef"
        :model="goodsForm"
        :rules="goodsFormRules"
        label-width="70px"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品名称" prop="goods_name">
              <el-input v-model="goodsForm.goods_name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="价格" prop="goods_price">
              <el-input v-model.number="goodsForm.goods_price"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="数量" prop="goods_number">
              <el-input v-model.number="goodsForm.goods_number"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="重量" prop="goods_weight">
              <el-input v-model.number="goodsForm.goods_weight"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      searchInput: "",
      goodsData: [],
      queroInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: null,
      editDialogVisible: false,
      goodsForm: [],
      goodsFormRules: {
        goods_name: [{ required: true, message: "不能为空", trigger: "blur" }],
        goods_price: [{ required: true, message: "不能为空", trigger: "blur" }],
        goods_number: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
        goods_weight: [
          { required: true, message: "不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    // 添加商品
    addGoods() {
      this.$router.push("/goods/add");
    },
    // 编辑表单
    edit(row) {
      this.goodsForm = row;
      this.editDialogVisible = true;
    },
    // 编辑表单的确定按钮
    editGoodsBtn() {
      this.$refs.goodsFormRef.validate(async (value) => {
        if (!value) return;
        // let goodsInfo = {
        //   goods_name: this.goodsForm.goods_name,
        //   goods_price: this.goodsForm.goods_price,
        //   goods_number: this.goodsForm.goods_number,
        //   goods_weight: this.goodsForm.goods_weight,
        // };
        // const { data: res } = await this.$http.put(
        //   `goods/${this.goodsForm.goods_id}`,
        //   goodsInfo
        // );
        // if (res.meta.status !== 201) {
        //   return this.$message.error("编辑失败");
        // }
        // this.$message.success("编辑失败");
        // this.getGoodsList();
        // this.editDialogVisible = false;
      });
    },
    // 关闭dialog时清空表单
    clearGoodsForm() {
      this.$refs.goodsFormRef.resetFields();
    },
    // 删除表格某行
    del(goodsId) {
      this.$confirm("确定删除此商品?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete("goods/" + goodsId);
          if (res.meta.status !== 200) {
            return this.$message.error("删除失败");
          }
          this.$message.success("删除成功");
          this.getGoodsList();
        })
        .catch(() => {});
    },
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queroInfo,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取失败");
      }
      this.goodsData = res.data.goods;
      this.total = res.data.total;
    },
    handleSizeChange(newPageSize) {
      this.queroInfo.pagesize = newPageSize;
      this.getGoodsList();
    },
    handleCurrentChange(newCurrent) {
      this.queroInfo.pagenum = newCurrent;
      this.getGoodsList();
    },
  },
  created() {
    this.getGoodsList();
  },
};
</script>

<style lang="less" scoped>
</style>